package com.primaryedu.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import com.primaryedu.R;
import com.primaryedu.bean.RepairBean;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 我的报修
 *
 * @author jun.wang
 * @description
 * @data 2016年12月14日
 */
public class MyRepairAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    private List<RepairBean> list = new ArrayList<>();

    public MyRepairAdapter(Context context, List<RepairBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateView(List<RepairBean> nowList) {
        this.list = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_my_repair, null);

            viewHolder.text_title = (TextView) convertView.findViewById(R.id.text_title);
            viewHolder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            viewHolder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        RepairBean bean = list.get(position);
        viewHolder.text_title.setText(bean.getContent());
        viewHolder.text_date.setText(bean.getCreateDate());

        if (!TextUtils.isEmpty(bean.getStatus())) {
            if (bean.getStatus().equals("1")) {
                viewHolder.image.setBackgroundResource(R.drawable.icon_to_manage);
            } else if (bean.getStatus().equals("2")) {
                viewHolder.image.setBackgroundResource(R.drawable.icon_manageing);
            } else {
                viewHolder.image.setBackgroundResource(R.drawable.icon_managed);
            }
        } else {
            viewHolder.image.setBackgroundResource(R.drawable.icon_to_manage);
        }


        return convertView;
    }

    class ViewHolder {
        TextView text_title;
        TextView text_date;
        ImageView image;
    }
}
